﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
//using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// Summary description for MaHoa
/// </summary>
public class MaHoa
{
    private string keyValue = "binhminh";
    public string Key
    {
        set { keyValue = value; }
        get { return keyValue; }
    }
    public string Code(string toEncrypt)
    {
        byte[] keyArray;
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

        string key = "binhminh";

        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        hashmd5.Clear();

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        tdes.Clear();
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }
    public string Mahoa(string password)
    {
        string encryptPassword = password + keyValue;
        byte[] passwordBytes = Encoding.UTF8.GetBytes(encryptPassword);

        HashAlgorithm hash = new MD5CryptoServiceProvider();
        byte[] hashBytes = hash.ComputeHash(passwordBytes);

        encryptPassword = Convert.ToBase64String(passwordBytes);
        return encryptPassword;
    }
    public string GiaiMa(string password)
    {
        string originalPassword = "";
        byte[] inputByteArray = Convert.FromBase64String(password);

        originalPassword = Encoding.UTF8.GetString(inputByteArray);

        return originalPassword.Substring(0,
        originalPassword.Length - keyValue.Length);
    }
}
